package ${basePkg}.web.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;

import ${basePkg}.anno.RequiredPermission;
import ${basePkg}.domain.${className};
import ${basePkg}.domain.JsonMsg;
import ${basePkg}.query.QueryObject;
import ${basePkg}.service.I${className}Service;
import lombok.Getter;

@Controller("${objectName}Action")
@Scope("prototype")
public class ${className}Action extends BaseAction{

	private static final long serialVersionUID = 26674362125140616L;

	@Autowired
	private I${className}Service ${objectName}Service;

	@Getter
	private ${className} ${objectName} = new ${className}();

	@Getter
	private QueryObject qo = new QueryObject();

	@Override
	@RequiredPermission("${entityName}列表")
	@InputConfig(methodName="input")
	public String execute() {
		try {
			ActionContext.getContext().put("objs", ${objectName}Service.query(qo));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return LIST;
	}
	
	@RequiredPermission("${entityName}编辑")
	public String input() throws Exception {
		if (${objectName}.getId() != null) {
			${objectName} = ${objectName}Service.get(${objectName}.getId());
		}
		return INPUT;
	}
	
	@RequiredPermission("${entityName}保存和更新")
	public String saveOrUpdate() {
		if (${objectName}.getId() != null) {
			try {
				${objectName}Service.update(${objectName});
				addActionMessage("更新成功!");
			} catch (Exception e) {
				e.printStackTrace();
				addActionError("系统异常,更新失败!");
			}
		} else {
			try {
				${objectName}Service.save(${objectName});
				addActionMessage("保存成功!");
			} catch (Exception e) {
				e.printStackTrace();
				addActionError("系统异常,保存失败!");
			}
		}
		return SUCCESS;
	}

	
	
	@RequiredPermission("${entityName}删除")
	public String delete() {
		try {
			if (${objectName}.getId() != null) {
				${objectName}Service.delete(${objectName}.getId());
				putJSON(new JsonMsg(true, "删除成功"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			putJSON(new JsonMsg(false, "系统异常,删除失败!"));
		}
		return NONE;
	}
	
	public void prepareSaveOrUpdate() throws Exception {
		if (${objectName}.getId()!= null) {
			${objectName} = ${objectName}Service.get(${objectName}.getId());
		}
	}
	
	
}
